import 'package:flutter/material.dart';

class SnackbarDemo extends StatefulWidget {
  const SnackbarDemo({Key? key}) : super(key: key);

  @override
  _SnackbarDemoState createState() => _SnackbarDemoState();
}

class _SnackbarDemoState extends State<SnackbarDemo> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Padding(
        padding: EdgeInsets.all(16),
        child: SingleChildScrollView(
          child: Container(
            child: Column(
              children: [
                SizedBox(
                  height: 15,
                ),
                SnackbarWidget()
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class SnackbarWidget extends StatelessWidget {
  const SnackbarWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: FlatButton(
        child: Text("通知提醒"),
        onPressed: () {
          showNackbar(context);
        },
      ),
    );
  }

  showNackbar(BuildContext context) {
    Scaffold.of(context).showSnackBar(SnackBar(
      content: Text("嗨,您收到一条新的短信..."),
      action: SnackBarAction(
          label: "OK",
          onPressed: () {
            Navigator.pop(context);
          }),
    ));
  }
}
